import time

from logger import log
import psutil
from pydispatch import dispatcher

def check_nginx_process():
    """
    检查是否存在nginx进程
    """
    for proc in psutil.process_iter(['name']):
        if proc.info['name'].lower() == 'nginx.exe':
            return True
    return False


def main():
    log.info("开始nginx心跳...")

    try:
        while True:
            if check_nginx_process():
                log.info("检测到nginx进程")
                dispatcher.send("nginx_heartbeat")
            else:
                log.info("未检测到nginx进程")
            time.sleep(30)  # 主线程保持运行
    except Exception as e:
        log.info(f"程序已终止。{e}")


if __name__ == "__main__":
    main()
